package com.asa.voice.mp3.asa.instream;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.util.HashMap;

public class HttpConnection {
  private Socket socket = new Socket();
  
  private InputStream instream;
  
  private HashMap<String, String> map = new HashMap<>();
  
  private String response;
  
  private String StatusLine;
  
  private int ResponseCode;
  
  private String ResponseMessage;
  
  private long ContentLength;
  
  public void open(URL paramURL, String paramString) throws IOException {
    String str1 = paramURL.getHost();
    if (paramString == null)
      paramString = "http://" + str1 + "/"; 
    String str2 = paramURL.getPath();
    int i = paramURL.getPort();
    if (i == -1)
      i = 80; 
    this.socket.setSoTimeout(5000);
    this.socket.connect(new InetSocketAddress(str1, i), 5000);
    PrintWriter printWriter = new PrintWriter(this.socket.getOutputStream(), true);
    printWriter.println("GET " + str2 + " HTTP/1.1");
    printWriter.println("Host: " + str1);
    printWriter.println("Referer: " + paramString);
    printWriter.println("Accept: */*");
    printWriter.println("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    printWriter.println("Connection: Keep-Alive");
    printWriter.println();
    this.instream = this.socket.getInputStream();
    getResponse();
  }
  
  private void getResponse() throws IOException {
    byte[] arrayOfByte = new byte[4096];
    int j = 0;
    byte b;
    int i;
    for (b = 0; b < -1&& j != 218762506 && (//asa:b < -1这是我自己补的
      i = this.instream.read()) != -1; b++) {
      arrayOfByte[b] = (byte)i;
      j <<= 8;
      j |= i;
    } 
    if (j != 218762506)
      throw new IOException("HTTP response header not found."); 
    this.response = new String(arrayOfByte, 0, b);
    String[] arrayOfString = this.response.split("\r\n");
    if (arrayOfString.length < 1)
      throw new IOException("Illegal response header."); 
    this.StatusLine = arrayOfString[0];
    parseStatusLine();
    for (String str : arrayOfString) {
      String[] arrayOfString1 = str.split(": ");
      if (arrayOfString1.length == 2)
        this.map.put(arrayOfString1[0], arrayOfString1[1]); 
    } 
    try {
      this.ContentLength = Long.parseLong(this.map.get("Content-Length"));
    } catch (NumberFormatException numberFormatException) {
      this.ContentLength = -1L;
    } 
  }
  
  private void parseStatusLine() throws IOException {
    String[] arrayOfString = this.StatusLine.split(" ");
    if (arrayOfString.length < 3)
      throw new IOException("Illegal response status-line."); 
    try {
      this.ResponseCode = Integer.parseInt(arrayOfString[1]);
      this.ResponseMessage = arrayOfString[2];
      for (byte b = 3; b < arrayOfString.length; b++)
        this.ResponseMessage += " " + arrayOfString[b]; 
    } catch (NumberFormatException numberFormatException) {
      this.ResponseCode = -1;
      throw new NumberFormatException("Illegal Response-Code: -1");
    } 
  }
  
  public InputStream getInputStream() {
    return this.instream;
  }
  
  public int getResponseCode() {
    return this.ResponseCode;
  }
  
  public long getContentLength() {
    return this.ContentLength;
  }
  
  public String getResponseMessage() {
    return this.ResponseMessage;
  }
  
  public String getHeaderField(String paramString) {
    return this.map.get(paramString);
  }
  
  public void printResponse() {
    if (this.response != null)
      System.out.println(this.response); 
  }
  
  public void close() throws IOException {
    this.map.clear();
    this.socket.close();
  }
}